import 'package:aitao_flutter/common_ui/age_widget.dart';
import 'package:aitao_flutter/common_ui/back_nav.dart';
import 'package:aitao_flutter/common_ui/circle_avatar.dart';
import 'package:aitao_flutter/style/box_style.dart';
import 'package:aitao_flutter/style/edgeInsets_style.dart';
import 'package:aitao_flutter/style/text_style.dart';
import 'package:flutter/material.dart';

import '../../common_ui/empty_view.dart';
import '../../model/invite_person_model.dart';
import '../../request/api.dart';
import '../../request/requestManager.dart';
import '../../utils/log_utils.dart';

class InviteListPage extends StatefulWidget {
  const InviteListPage({Key? key}) : super(key: key);

  @override
  State<InviteListPage> createState() => _InviteListPageState();
}

class _InviteListPageState extends State<InviteListPage> {
  List<InvitePersonModel> inviteList = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BackNavigation(
        title: '我邀请的人',
      ),
      body: Container(
        padding: EdgeInsetsStyle.all(12),
        child: inviteList.isEmpty
            ? EmptyView()
            : ListView.builder(
                itemBuilder: (context, index) {
                  return _inviteUserWiget(inviteList[index]);
                },
                itemCount: inviteList.length,
              ),
      ),
    );
  }

  Widget _inviteUserWiget(InvitePersonModel model) {
    return Container(
      padding: EdgeInsetsStyle.customMargin(5, 5, 5, 15),
      child: Row(
        children: [
          CircleAvatarWidget(
            url: model.avatar ?? '',
            gender: model.gender ?? 1,
            imgWidth: 50,
            imgHeight: 50,
          ),
          Box.hBox10,
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Text(
                    model.nickname ?? '',
                    style: Styles.normalText_14,
                  ),
                  Box.hBox10,
                  AgeWidget(
                    age: model.age ?? 1,
                    gender: model.gender ?? 1,
                  )
                ],
              ),
              Box.vBox5,
              Text(
                'ID:${model.usercode}',
                style: Styles.greyText_12,
              )
            ],
          ),
          const Spacer(),
          Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Text(
                '贡献爱心:${model.inviterIncome}',
                style: Styles.normalText_14,
              ),
              Box.vBox5,
              Text(
                model.addTime ?? '',
                style: Styles.greyText_13,
              )
            ],
          )
        ],
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    getInviteList();
  }

  void getInviteList() async {
    var result = await RequestManager().post(API.inviteList);
    if (result != null && result['code'] == 0) {
      WJLogUtils.wjLog(result);
      List tempList = result['data']['list'];
      for (var element in tempList) {
        inviteList.add(InvitePersonModel.fromJson(element));
      }
      setState(() {});
    }
  }
}
